#include "notification_manager.h"

OPEN_JLIB_NS


auto NotificationManager::registerListener (const name_type& channel, func_type callback) noexcept -> id_type {
  auto &queue = _channels[channel];
  auto id = obtainId();
  queue.emplace(id, std::forward<func_type>(callback));
  return id;
}

bool NotificationManager::unregisterListener (const name_type& channel, id_type id) noexcept {
  auto &queue = _channels[channel];
  return queue.erase(id) > 0;
}

bool NotificationManager::cleanChannel (const name_type& channel) noexcept {
  _channels.erase(channel) > 0;
}

void NotificationManager::cleanAll () noexcept {
  _channels.clear();
}

uwl NotificationManager::broadcast (const name_type& channel, data_type data) {
  uwl responses = 0;
  auto &queue = _channels[channel];
  for (auto &node : queue) {
    if (!node.second)
      continue;
    node.second(data);
    ++responses;
  }
  return responses;
}


CLOSE_JLIB_NS
